package movee.domain.mapper;

import movee.domain.constants.BackupDeviceStatus;
import movee.domain.dao.BackupDeviceDao;
import movee.domain.mquery.BackupDeviceFindAllIdsQuery;
import movee.domain.query.BackupDeviceSearchQuery;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.sql.Timestamp;
import java.util.List;

/**
 *
 *
 * @author movee
 */
@Mapper
public interface BackupDeviceMapper extends BaseMapper<BackupDeviceDao> {

    List<BackupDeviceDao> findAll(BackupDeviceSearchQuery query);

    List<BackupDeviceDao> findAllWithTableName(BackupDeviceSearchQuery query);

    List<Long> findAllIds(BackupDeviceFindAllIdsQuery query);

    long countAll(BackupDeviceSearchQuery query);

    Integer updateStatusByIds(@Param(value = "ids") List<Long> ids,
                              @Param(value = "status") BackupDeviceStatus status);

    @ResultMap("BackupDeviceMap")
    @Select({"select * from t_backup_device where work_node = #{workNode} and status = #{status}"})
    List<BackupDeviceDao> findByWorkNodeAndStatus(@Param(value = "workNode") String workNode,
                                                  @Param(value = "status") BackupDeviceStatus status);


    @Update({"update t_backup_device set status = #{status} where id = #{id}"})
    Integer updateStatusById(@Param(value = "id") Long id,
                             @Param(value = "status") BackupDeviceStatus status);


    @ResultMap("BackupDeviceMap")
    @Select({"select * from t_backup_device where",
            "mgmt_ip = #{mgmtIp} and start_time >= #{startTime} and start_time < #{endTime} and status = #{status}"})
    List<BackupDeviceDao> findByMgmtIpAndStartTimeBetween(@Param(value = "mgmtIp") String mgmtIp,
                                                          @Param(value = "startTime") Timestamp startTime,
                                                          @Param(value = "endTime") Timestamp endTime,
                                                          @Param(value = "status") BackupDeviceStatus status);


    @Select(value = "select `create_time` from `t_backup_device` order by `id` asc limit 1")
    Timestamp findDeviceTaskOldestCreateTime();

    @Select({"<script>",
                "select distinct id from `t_backup_device`",
                "where task_id in",
                    "<foreach collection='taskIds' item='item' open='(' separator=',' close=')'>",
                        "#{item}",
                    "</foreach>",
                "and create_time &gt;= #{lowTime} and create_time &lt; #{highTime}",
            "</script>"})
    List<Long> findDistinctIds(@Param(value = "taskIds") List<Long> taskIds,
                                       @Param(value = "lowTime") Timestamp lowTime,
                                       @Param(value = "highTime") Timestamp highTime);
}
